package com.laoniu.service.goods;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laoniu.dto.request.GoodsStockRequestDto;
import com.laoniu.entity.GoodsReturn;
import com.laoniu.entity.SecurityUser;
import com.laoniu.mapper.GoodsReturnMapper;
import com.laoniu.utils.SpringSecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 商品退货表 服务实现类
 * </p>
 *
 * @author lxl
 * @since 2023-12-17
 */
@Service
public class GoodsReturnService extends ServiceImpl<GoodsReturnMapper, GoodsReturn> {

    @Resource
    private GoodsReturnMapper goodsReturnMapper;

    public Page<GoodsReturn> listPage(GoodsStockRequestDto requestDto) {
        SecurityUser user = SpringSecurityUtils.getUser();
        Page<GoodsReturn> page = new Page<>(requestDto.getPageNo(), requestDto.getPageSize());
        LambdaQueryWrapper<GoodsReturn> wrapper = Wrappers.lambdaQuery(GoodsReturn.class);
        wrapper.eq(Objects.nonNull(requestDto.getSupplierId()), GoodsReturn::getSupplierId, requestDto.getSupplierId());
        wrapper.eq(Objects.nonNull(requestDto.getGoodsId()), GoodsReturn::getGoodsId, requestDto.getGoodsId());
        wrapper.eq(!user.isAdmin(), GoodsReturn::getOpId, user.getId());
        wrapper.orderByDesc(GoodsReturn::getId);
        return goodsReturnMapper.selectPage(page, wrapper);
    }
}
